﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AgendaManager.AgendaService;

namespace AgendaManager
{
    public partial class frmSeleccionCarpeta : Form
    {
        public Obra Obra { get; set; }
        public List<Carpeta> Carpetas { get; set; }

        public frmSeleccionCarpeta()
        {
            InitializeComponent();
        }

        private void frmSeleccionCarpeta_Load(object sender, EventArgs e)
        {
            if (Obra != null)
            {
                txObra.Text = Obra.Nombre;

                Carpetas = ConnectHelper.ws.RetrieveEntitiesWhere(new Carpeta(),
                    "ObraId = " + Obra.Id.ToString()).Cast<Carpeta>().ToList();

                gvCarpetas.DataSource = null;
                gvCarpetas.DataSource = Carpetas;
                gvCarpetas.Columns["Id"].Visible = false;
                gvCarpetas.Columns["ObraId"].Visible = false;
                gvCarpetas.Columns["Tipo"].Width = 513;
            }
        }

        internal void SetValues(Obra obra)
        {
            try
            {
                Obra = obra;

                txObra.Text = Obra.Nombre;

                Carpetas = ConnectHelper.ws.RetrieveEntitiesWhere(new Carpeta(),
                    "ObraId = " + Obra.Id.ToString()).Cast<Carpeta>().ToList();

                gvCarpetas.DataSource = null;
                gvCarpetas.DataSource = Carpetas;
                gvCarpetas.Columns["Id"].Visible = false;
                gvCarpetas.Columns["ObraId"].Visible = false;
                gvCarpetas.Columns["Tipo"].Width = 513;
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        private void btnSeleccionar_Click(object sender, EventArgs e)
        {
            if (gvCarpetas.SelectedRows.Count == 1)
            {
                Carpeta carpeta = new Carpeta();
                carpeta = (Carpeta)gvCarpetas.SelectedRows[0].DataBoundItem;

                if (carpeta.Tipo == "Fotos")
                {
                    var f = FormsHelper.ShowForm<frmSemana>();
                    f.SetValues(Obra, carpeta);
                }

                if (carpeta.Tipo == "Planos")
                {
                    var f = FormsHelper.ShowForm<frmPlanos>();
                    f.SetValues(Obra, carpeta);
                    
                }

                if (carpeta.Tipo == "Ivos y Notas")
                {
                    var f = FormsHelper.ShowForm<frmIvoNota>();
                    f.SetValues(Obra, carpeta);
                }


            }
            else
            {
                MessageBox.Show("Debe seleccionar una Carpeta.",
                            "Carpeta.",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
    }
}
